package cn.com.bluemoon.daps.model.utils.factory.impl.key;

import cn.com.bluemoon.daps.model.dto.SubordinateKeyDto;
import cn.com.bluemoon.daps.model.entity.DapDataModelSubordinateKey;
import cn.com.bluemoon.daps.model.utils.SqlUtils;
import cn.com.bluemoon.daps.model.utils.factory.KeyScriptFactory;

import java.util.Arrays;
import java.util.List;

/**
 * @author karry
 * @Data 2021/9/7
 * @Time:10:46
 */
public abstract class AbsKeyScript implements KeyScriptFactory {

    public final static List KEY_VALUE = Arrays.asList(1,2);
    // 通用逻辑
    public String createKeyPublicCode(List<SubordinateKeyDto> keys, String single){
        // 对索引列表进行加工
        List<DapDataModelSubordinateKey> list = SqlUtils.getFieldData(keys, single);
        StringBuffer keyBuffer = new StringBuffer();
        for (DapDataModelSubordinateKey key : list){
            String format = String.format("KEY %s%s%s (%s)",  single, key.getKeyName(), single, key.getFieldIds());
            keyBuffer.append(KEY_VALUE.contains(key.getKeyType()) ? "" : "UNIQUE ").append(format);
            keyBuffer.append(key.getKeyMethod() == 1 ? "," : key.getKeyMethod() == 2 ? " USING BTREE," :
                    key.getKeyMethod() == 3 ? " USING HASH," : " USING FULLTEXT,");
        }
        return keyBuffer.toString();
    }
}
